﻿IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[esf_files].[P_DeleteFileReference]') AND type in (N'P', N'PC'))
	DROP PROCEDURE [esf_files].[P_DeleteFileReference]
GO


CREATE PROCEDURE esf_files.P_DeleteFileReference
	@pFileReferenceID INT = NULL
	,@pFileReferenceURL VARCHAR (MAX) = NULL
AS

	IF @pFileReferenceID IS NULL
	BEGIN
		SET @pFileReferenceID = (SELECT ID FROM esf_files.FileReference WHERE URL = @pFileReferenceURL)
	END

	DELETE FROM esf_files.FileReferenceLevel WHERE FileReferenceID = @pFileReferenceID
	DELETE FROM esf_files.FileReferenceWordHit WHERE FileReferenceID = @pFileReferenceID
	DELETE FROM esf_files.FileReference WHERE ID = @pFileReferenceID

GO
